﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
namespace QLDT
{

    public partial class QLPhanCongDT : DevExpress.XtraEditors.XtraForm
    {
        public bool TTThemSua = true;
        public bool BoolMaDT = true;
        public bool BoolMaSV = true;
        public bool BoolMaLoaiDT = true;
        private string ID_PCDT;
        public QLPhanCongDT()
        {
            InitializeComponent();
        }

        private void QLPhanCongDT_Load(object sender, EventArgs e)
        {
            load();
        }
        public void createGridlookup()
        {
            //gridLookUp_MaKH 
            gridLookUp_MaKH.EditValue = null;
            gridLookUp_MaKH.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUp_MaKH.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUp_MaKH.Properties.ValueMember = "MaKH";
            gridLookUp_MaKH.Properties.DisplayMember = "MaKH";
            gridLookUp_MaKH.Properties.View.BestFitColumns();
            gridLookUp_MaKH.Properties.PopupFormWidth = 400;
            gridLookUp_MaKH.Properties.NullText = "Chọn khóa học";
            gridLookUp_MaKH.Properties.DataSource = BUS_KhoaHoc.SelectKhoaHoc();

            // gridLookUp_MaLoaiDT
            gridLookUp_MaLoaiDT.EditValue = null;
            gridLookUp_MaLoaiDT.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUp_MaLoaiDT.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUp_MaLoaiDT.Properties.ValueMember = "MaLoaiDT";
            gridLookUp_MaLoaiDT.Properties.DisplayMember = "MaLoaiDT";
            gridLookUp_MaLoaiDT.Properties.View.BestFitColumns();
            gridLookUp_MaLoaiDT.Properties.PopupFormWidth = 400;
            gridLookUp_MaLoaiDT.Properties.NullText = "Chọn loại đề tài";
            gridLookUp_MaLoaiDT.Properties.DataSource = BUS_LoaiDT.selectLoaiDeTai();
            //gridLookUpMaSV

            gridLookUpMaSV.EditValue = null;
            gridLookUpMaSV.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUpMaSV.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUpMaSV.Properties.ValueMember = "MaSV";
            gridLookUpMaSV.Properties.DisplayMember = "MaSV";
            gridLookUpMaSV.Properties.View.BestFitColumns();
            gridLookUpMaSV.Properties.PopupFormWidth = 400;
            gridLookUpMaSV.Properties.NullText = "Chọn mã sinh viên";
            gridLookUpMaSV.Properties.DataSource = BUS_SinhVien.SelectSinhVien();

            //gridLookUpMaDT
            gridLookUpMaDT.EditValue = null;
            gridLookUpMaDT.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUpMaDT.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUpMaDT.Properties.ValueMember = "MaDT";
            gridLookUpMaDT.Properties.DisplayMember = "MaDT";
            gridLookUpMaDT.Properties.View.BestFitColumns();
            gridLookUpMaDT.Properties.PopupFormWidth = 400;
            gridLookUpMaDT.Properties.NullText = "Chọn mã đề tài";
            gridLookUpMaDT.Properties.DataSource = BUS_DeTai.selectDeTai_ALL();

            gridLookUp_LoaiDT.EditValue = null;
            gridLookUp_LoaiDT.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUp_LoaiDT.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUp_LoaiDT.Properties.ValueMember = "MaLoaiDT";
            gridLookUp_LoaiDT.Properties.DisplayMember = "MaLoaiDT";
            gridLookUp_LoaiDT.Properties.View.BestFitColumns();
            gridLookUp_LoaiDT.Properties.PopupFormWidth = 400;
            gridLookUp_LoaiDT.Properties.NullText = "Chọn loại đề tài";
            gridLookUp_LoaiDT.Properties.DataSource = BUS_LoaiDT.selectLoaiDeTai();
        }
        public void load()
        {
            
            layoutControlPCDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layoutControlPCDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            filter();
           // txt_IP_PCDT.Enabled = false;
            gridLookUpMaDT.Enabled = false;
            gridLookUpMaSV.Enabled = false;
            txt_MaCN.Enabled = false;
            txt_MaKH.Enabled = false;
            txt_MaLoaiDT.Enabled = false;
            txt_ngaynhan.Enabled = false;
            date_ngayhethan.Enabled = false;
            txt_DiemNHD.Enabled = false;
            txt_DiemPB.Enabled = false;
            gridLookUp_MaKH.Enabled = true;
            gridLookUp_MaLoaiDT.Enabled = true;
            radio_DTDK.Enabled = true;
            gridQLPhanCongDT.Enabled = true;


            simpleBt_Capnhat.Enabled = simpleBt_Huy.Enabled = false;
            if (gv_QLPCDT.RowCount < 1)
            {
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = false;
            }
            else
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = true;
            simpleBt_Them.Enabled = true;
            Bindings();
            createGridlookup();
            layout_radio.Size = new System.Drawing.Size(1023, 30);
            if (gv_QLPCDT.RowCount >= 1)
            {
                simpleBt_Sua.Enabled = true;
                simpleBt_Xoa.Enabled = true;
            }
            else
            {
                simpleBt_Xoa.Enabled = simpleBt_Sua.Enabled = false;
            }
        }

        private void Bindings()
        {
          //  txt_IP_PCDT.DataBindings.Clear();
            txt_MaCN.DataBindings.Clear();
            txt_MaKH.DataBindings.Clear();
            //txt_MaLoaiDT.DataBindings.Clear();
            txt_ngaynhan.DataBindings.Clear();
            date_ngayhethan.DataBindings.Clear();
            txt_DiemNHD.DataBindings.Clear();
            txt_DiemPB.DataBindings.Clear();


           // txt_IP_PCDT.DataBindings.Add("Text", gridQLPhanCongDT.DataSource, "ID_PCDT");
            txt_MaCN.DataBindings.Add("Text", gridQLPhanCongDT.DataSource, "MaCN");
            txt_MaKH.DataBindings.Add("Text", gridQLPhanCongDT.DataSource, "MaKH");
           // txt_MaLoaiDT.DataBindings.Add("Text", gridQLPhanCongDT.DataSource, "MaLoaiDT");
            txt_ngaynhan.DataBindings.Add("Text", gridQLPhanCongDT.DataSource, "NgayNhan");
            date_ngayhethan.DataBindings.Add("Text", gridQLPhanCongDT.DataSource, "NgayHetHan_ChonDT");
            txt_DiemNHD.DataBindings.Add("Text", gridQLPhanCongDT.DataSource, "DiemNHD");
            txt_DiemPB.DataBindings.Add("Text", gridQLPhanCongDT.DataSource, "DiemPB");

        }
        public void select_PCDT_Makh_maloai(string makh, string loaidt)
        {
            gridQLPhanCongDT.DataSource = BUS_QLPhanCongDT.QlPhanCongDt_TheoMaKH_MaloaiDt(makh, loaidt);

        }
        public void loadDSMaLoaiDT_theokhoahoc(string makhoahoc)
        {
            gridLookUp_MaLoaiDT.EditValue = null;
            gridLookUp_MaLoaiDT.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookUp_MaLoaiDT.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookUp_MaLoaiDT.Properties.ValueMember = "MaLoaiDT";
            gridLookUp_MaLoaiDT.Properties.DisplayMember = "MaLoaiDT";
            gridLookUp_MaLoaiDT.Properties.View.BestFitColumns();
            gridLookUp_MaLoaiDT.Properties.PopupFormWidth = 400;
            gridLookUp_MaLoaiDT.Properties.NullText = "Chọn loai đề tài";
            gridLookUp_MaLoaiDT.Properties.DataSource = BUS_KhoaHoc.SelectKhoaHoc_theokhoa(makhoahoc);
        }
        private void gridLookUp_MaKH_EditValueChanged(object sender, EventArgs e)
        {
            /*string MaKH = gridLookUp_MaKH.EditValue.ToString();
            string MaLoaiDt;
            if (gridLookUp_MaLoaiDT.EditValue == null)
                MaLoaiDt = "%";
            else
                MaLoaiDt = gridLookUp_MaLoaiDT.EditValue.ToString();
            select_PCDT_Makh_maloai(MaKH, MaLoaiDt);*/
            filter();
        }

        private void gridLookUp_MaLoaiDT_EditValueChanged(object sender, EventArgs e)
        {
            /*string MaLoaiDt = gridLookUp_MaLoaiDT.EditValue.ToString();
            string MaKH;
            if (gridLookUp_MaKH.EditValue == null)
                MaKH = "%";
            else
                MaKH = gridLookUp_MaKH.EditValue.ToString();
            select_PCDT_Makh_maloai(MaKH, MaLoaiDt);
             */
            filter();
        }
        private void filter()
        {
            string loaidt = "%";
            string khoahoc = "%";
            if (gridLookUp_MaLoaiDT.EditValue != null)
            {
                loaidt = gridLookUp_MaLoaiDT.EditValue.ToString();
            }
            if (gridLookUp_MaKH.EditValue != null)
            {
                khoahoc = gridLookUp_MaKH.EditValue.ToString();
            }

            if (radio_DTDK.SelectedIndex == 0)
            {
                try
                {
                    gridQLPhanCongDT.DataSource = BUS_QLPhanCongDT.SelectPhanCongDT_TheoKhoaHoc(khoahoc, loaidt);

                }
                catch { }
            }
            else if (radio_DTDK.SelectedIndex == 1)
            {
                try
                {
                    gridQLPhanCongDT.DataSource = BUS_QLPhanCongDT.Select_DaDKDT(khoahoc, loaidt);
                }
                catch { }
            }
            else if (radio_DTDK.SelectedIndex == 2)
            {
                try
                {
                    gridQLPhanCongDT.DataSource = BUS_QLPhanCongDT.Select_ChuaDKDT(khoahoc, loaidt);
                }
                catch { }
            }
            else if (radio_DTDK.SelectedIndex == 3)
            {
                try
                {
                    gridQLPhanCongDT.DataSource = BUS_QLPhanCongDT.Select_DTDangThucHien(khoahoc, loaidt);
                }
                catch { }

            }
            else if (radio_DTDK.SelectedIndex == 4)
            {
                try
                {
                    gridQLPhanCongDT.DataSource = BUS_QLPhanCongDT.Select_DTThucHienXong(khoahoc, loaidt);
                }
                catch { }
            }
            if (gv_QLPCDT.RowCount >= 1)
            {
                simpleBt_Sua.Enabled = true;
                simpleBt_Xoa.Enabled = true;
            }
            else
            {
                simpleBt_Xoa.Enabled = simpleBt_Sua.Enabled = false;
            }
        }
        private void radio_DTDK_SelectedIndexChanged(object sender, EventArgs e)
        {
            filter();
        }



        private void simpleBt_Them_Click_1(object sender, EventArgs e)
        {

            layoutControlPCDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutControlPCDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_Capnhat.Enabled = true;
            simpleBt_Huy.Enabled = true;
            radio_DTDK.Enabled = false;
            gridLookUp_MaKH.Enabled = false;
            gridLookUp_MaLoaiDT.Enabled = false;

           //txt_ID_PCDT.DataBindings.Clear();
            gridLookUpMaDT.DataBindings.Clear();
            gridLookUpMaSV.DataBindings.Clear();
            txt_MaCN.DataBindings.Clear();
            txt_MaKH.DataBindings.Clear();
            txt_MaLoaiDT.DataBindings.Clear();
            txt_ngaynhan.DataBindings.Clear();
            date_ngayhethan.DataBindings.Clear();
            txt_DiemNHD.DataBindings.Clear();
            txt_DiemPB.DataBindings.Clear();
            gridLookUp_LoaiDT.DataBindings.Clear();

           //txt_ID_PCDT.Text = "";
            gridLookUpMaSV.Text = "";
            txt_MaCN.Text = "";
            txt_MaKH.Text = "";
            txt_MaLoaiDT.Text = "";
            gridLookUpMaSV.Text = "";
            txt_ngaynhan.Text = DateTime.Now.ToShortDateString();

            date_ngayhethan.Text = "";
            txt_DiemNHD.Text = "";
            txt_DiemPB.Text = "";
            gridLookUp_LoaiDT.Text = "";

          // txt_ID_PCDT.Enabled = true;
            gridLookUpMaDT.Enabled = true;
            gridLookUpMaSV.Enabled = true;
            gridLookUp_LoaiDT.Enabled = true;
            txt_MaCN.Enabled = false;
            txt_MaKH.Enabled = false;
            txt_MaLoaiDT.Enabled = false;
            txt_ngaynhan.Enabled = false;
            date_ngayhethan.Enabled = true;
            txt_DiemNHD.Enabled = true;
            txt_DiemPB.Enabled = true;
            TTThemSua = true;
            layout_radio.Size = new System.Drawing.Size(1023, 30);
        }

        private void simpleBt_Capnhat_Click(object sender, EventArgs e)
        {
            if (!KTDulieu())
            {
                XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                return;
            }
            DTO_QLPhanCongDT PCDT = new DTO_QLPhanCongDT();
            //PCDT.ID_PCDT = int.Parse(txt_IP_PCDT.ToString());
            if (!TTThemSua)
            {
                if (BoolMaDT == false)
                {
                    PCDT.MaDT = gridLookUpMaDT.Properties.NullText;
                }
                else
                {
                    PCDT.MaDT = gridLookUpMaDT.EditValue.ToString();
                }
                if (BoolMaSV == false)
                {
                    PCDT.MaSV = gridLookUpMaSV.Properties.NullText;
                }
                else
                {
                    PCDT.MaSV = gridLookUpMaSV.EditValue.ToString();
                }
                if (BoolMaLoaiDT == false)
                {
                    PCDT.MaLoaiDT = gridLookUp_LoaiDT.Properties.NullText;
                }
                else
                {
                    PCDT.MaLoaiDT = gridLookUp_LoaiDT.EditValue.ToString();
                }
            }
            else
            {
                if (gridLookUpMaDT.EditValue == null)
                    PCDT.MaDT = "";
                else
                    PCDT.MaDT = gridLookUpMaDT.EditValue.ToString();
                if (gridLookUpMaSV.EditValue == null)
                {
                    XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                    return;
                }
                else
                    PCDT.MaSV = gridLookUpMaSV.EditValue.ToString();
                if (gridLookUp_LoaiDT.EditValue == null)
                {
                    XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                    return;
                }
                else
                    PCDT.MaLoaiDT = gridLookUp_LoaiDT.EditValue.ToString();
            }
            PCDT.MaCN = txt_MaCN.Text;
            PCDT.MaKH = txt_MaKH.Text;
            PCDT.NgayNhan = DateTime.Parse(txt_ngaynhan.Text);
            PCDT.NgayHetHan_ChonDT = DateTime.Parse(date_ngayhethan.Text);
            PCDT.DiemNHD = float.Parse(txt_DiemNHD.Text);
            PCDT.DiemPB = float.Parse(txt_DiemPB.Text);

            if (TTThemSua)
            {

                if (BUS_QLPhanCongDT.insertQLPhanCongDT(PCDT))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }
            else
            {
                 if (BUS_QLPhanCongDT.UpdatePCDT(PCDT, ID_PCDT))
                   load();
                else
                 XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }

        }

        private bool KTDulieu()
        {

            //if (txt_ID_PCDT.Text == "")
           //     return false;
           
            if (gridLookUpMaSV.Text == "")
                return false;
            if (gridLookUp_LoaiDT.Text == "")
                return false;
            
            if (txt_MaKH.Text == "") 
                return false;
            if (txt_ngaynhan.Text == "")
                return false;
            if (date_ngayhethan.Text == "")
                return false;
            if (txt_DiemNHD.Text == "")
                return false;
            if (txt_DiemPB.Text == "")
                return false;
            return true;

        }

        private void simpleBt_Huy_Click(object sender, EventArgs e)
        {
            load();
        }

        private void gridLookUpMaDT_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                DTO_DeTai dt = BUS_DeTai.selectDeTai_TheoMaDT(gridLookUpMaDT.EditValue.ToString())[0];
               // txt_MaLoaiDT.Text = dt.MaLoaiDT;
               
                txt_MaCN.Text = dt.MaCN;
                BoolMaDT = true;
            }
            catch { }
        }

        private void gridLookUpMaSV_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                DTO_Lop sv = BUS_Lop.SelectKH_Theolop(gridLookUpMaSV.EditValue.ToString())[0];
                txt_MaKH.Text = sv.MaKH;
                BoolMaSV = true;
            }
            catch { }
        }

        private void simpleBt_Sua_Click(object sender, EventArgs e)
        {
            radio_DTDK.Enabled = false;
            gridLookUp_MaKH.Enabled = false;
            gridLookUp_MaLoaiDT.Enabled = false;
            layoutControlPCDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layoutControlPCDT.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_Capnhat.Enabled = true;
            simpleBt_Huy.Enabled = true;
            ID_PCDT = gv_QLPCDT.GetRowCellValue(gv_QLPCDT.FocusedRowHandle, "ID_PCDT").ToString();

            txt_MaCN.DataBindings.Clear();
            txt_MaKH.DataBindings.Clear();
            txt_MaLoaiDT.DataBindings.Clear();
            txt_ngaynhan.DataBindings.Clear();
            date_ngayhethan.DataBindings.Clear();
            txt_DiemNHD.DataBindings.Clear();
            txt_DiemPB.DataBindings.Clear();
   
            gridLookUpMaDT.Enabled = true;
            gridLookUpMaSV.Enabled = true;
            gridLookUp_LoaiDT.Enabled = true;
            txt_MaCN.Enabled = false;
            txt_MaKH.Enabled = false;
            txt_MaLoaiDT.Enabled = false;
            txt_ngaynhan.Enabled = false;
            date_ngayhethan.Enabled = true;
            txt_DiemNHD.Enabled = true;
            txt_DiemPB.Enabled = true;
           
            TTThemSua = false;
            gridLookUpMaDT.Properties.NullText = gv_QLPCDT.GetRowCellValue(gv_QLPCDT.FocusedRowHandle, "MaDT").ToString();
            gridLookUpMaSV.Properties.NullText = gv_QLPCDT.GetRowCellValue(gv_QLPCDT.FocusedRowHandle, "MaSV").ToString();
            gridLookUp_LoaiDT.Properties.NullText = gv_QLPCDT.GetRowCellValue(gv_QLPCDT.FocusedRowHandle, "MaLoaiDT").ToString();
            BoolMaDT = false;
            BoolMaSV = false;


        }

        private void simpleBt_Xoa_Click(object sender, EventArgs e)
        {
            bool kq;
            ID_PCDT = gv_QLPCDT.GetRowCellValue(gv_QLPCDT.FocusedRowHandle, "ID_PCDT").ToString();
            if (MessageBox.Show("Bạn có chắc sẽ đề tài " + ID_PCDT+ " hay không, nếu xóa thì toàn bộ thông thông tin liên quan sẽ mất.", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                kq = BUS_QLPhanCongDT.deleteQLPCDT(ID_PCDT);
                if (!kq)
                    XtraMessageBox.Show("Xóa thất bại", "Thông báo");
                else
                {
                    load();
                }
            }
            else
                load();
        }
      
        private void gridLookUp_LoaiDT_EditValueChanged(object sender, EventArgs e)
        {
            if (gridLookUp_LoaiDT.EditValue != null)
            {
                string loaidt = gridLookUp_LoaiDT.EditValue.ToString();
                gridLookUpMaDT.EditValue = null;
                gridLookUpMaDT.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
                gridLookUpMaDT.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
                gridLookUpMaDT.Properties.ValueMember = "MaDT";
                gridLookUpMaDT.Properties.DisplayMember = "MaDT";
                gridLookUpMaDT.Properties.View.BestFitColumns();
                gridLookUpMaDT.Properties.PopupFormWidth = 400;
                gridLookUpMaDT.Properties.NullText = "Chọn đề tài";
                gridLookUpMaDT.Properties.DataSource = BUS_DeTai.ChonDT_TheoLoaiDT(loaidt);
            }
          
        }



    }
}